//
//  ConversionOptionView.swift
//  SCA
//
//  Created by tjun on 2025/9/23.
//

import UIKit
import SnapKit

class ConversionOptionView: UIView {
    
    enum OptionType {
        case size
        case type
        case color
        case country
    }
 
    let type: OptionType
    var selectModel: ConversionOptionModel? {
        didSet {
            textLabel.text = selectModel?.value
        }
    }
    
    let titleLabel = UILabel(font: UIFontMediumMake(15), textColor: UIColor.white)
    private let tipLabel = UILabel(font: UIFontMake(12), textColor: UIColorMake(0x797777))
    let textLabel = UILabel(font: UIFontMake(15), textColor: UIColor.white)
    private let arrowImageView = UIImageView(image: UIImage(named: "downIcon"))
    
    let contentView = UIView()
    
    var title: String? {
        didSet {
            titleLabel.text = title
        }
    }
    
    var titleAttributedText: NSAttributedString? {
        didSet {
            titleLabel.attributedText = titleAttributedText
        }
    }

    var tip: String? {
        didSet {
            tipLabel.text = tip
        }
    }
    
    var textFieldCornerRadius: CGFloat = 15 {
        didSet {
            contentView.layer.cornerRadius = textFieldCornerRadius
        }
    }
    
    init(type: OptionType) {
        self.type = type
        super.init(frame: .zero)
        
        setupUI()
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI 布局
    private func setupUI() {
        
        contentView.layer.cornerRadius = 15
        contentView.layer.borderWidth = 1
        contentView.backgroundColor = UIColorMake(0x2E3036)
        contentView.layer.borderColor = UIColorMake(0x2E3036).cgColor
        
        addSubview(titleLabel)
        addSubview(tipLabel)
        
        addSubview(contentView)
        contentView.addSubview(textLabel)
        contentView.addSubview(arrowImageView)
    }
    
    private func setupConstraints() {
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.left.equalToSuperview().offset(2)
        }
        
        contentView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.height.equalTo(44)
            make.left.right.equalToSuperview()
        }
                
        textLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
        
        tipLabel.snp.makeConstraints { make in
            make.top.equalTo(contentView.snp.bottom).offset(6)
            make.left.equalTo(titleLabel)
        }
    }

    
    func didBeginEditing() {
        animateBorderColor(toColor: UIColorMake(0x2078D5).cgColor)
    }

    func didEndEditing() {
        animateBorderColor(toColor: UIColorMake(0x2E3036).cgColor)
    }
    
    
    private func animateBorderColor(toColor: CGColor) {
        let animation = CABasicAnimation(keyPath: "borderColor")
        animation.fromValue = contentView.layer.borderColor
        animation.toValue = toColor
        animation.duration = 0.25  // 动画时长
        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        contentView.layer.add(animation, forKey: "borderColor")
        contentView.layer.borderColor = toColor
    }
}


class PhoneNumberOptionView: UIView, UITextFieldDelegate {
    
    let titleLabel = UILabel(font: UIFontMediumMake(15), textColor: UIColor.white)
    private let tipLabel = UILabel(font: UIFontMake(12), textColor: UIColorMake(0x797777))
    let textLabel = UILabel(font: UIFontMake(15), textColor: UIColor.white)
    private let arrowImageView = UIImageView(image: UIImage(named: "downIcon"))
    let textField = UITextField()

    let contentView = UIView()
    
    var title: String? {
        didSet {
            titleLabel.text = title
        }
    }
    
    var titleAttributedText: NSAttributedString? {
        didSet {
            titleLabel.attributedText = titleAttributedText
        }
    }

    var tip: String? {
        didSet {
            tipLabel.text = tip
        }
    }
    
    var textFieldCornerRadius: CGFloat = 15 {
        didSet {
            contentView.layer.cornerRadius = textFieldCornerRadius
        }
    }
    
    // MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 布局
    private func setupUI() {
        
        contentView.layer.cornerRadius = 15
        contentView.layer.borderWidth = 1
        contentView.backgroundColor = UIColorMake(0x2E3036)
        contentView.layer.borderColor = UIColorMake(0x2E3036).cgColor
        
        textField.delegate = self
        textField.textColor = .white
        textField.font = UIFont.systemFont(ofSize: 16)
        textField.returnKeyType = .done
        textField.clearButtonMode = .whileEditing
        textField.keyboardAppearance = ThemeManager.shared.currentStyle == .dark ? .dark : .light
        
        addSubview(titleLabel)
        addSubview(tipLabel)
        
        addSubview(contentView)
        contentView.addSubview(textField)
        contentView.addSubview(textLabel)
        contentView.addSubview(arrowImageView)
    }
    
    private func setupConstraints() {
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.left.equalToSuperview().offset(2)
        }
        
        contentView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.height.equalTo(44)
            make.left.right.equalToSuperview()
        }
                
        textLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.left.equalTo(textLabel.snp.right).offset(20)
            make.centerY.equalToSuperview()
        }
        
        textField.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(120)
        }
        
        tipLabel.snp.makeConstraints { make in
            make.top.equalTo(contentView.snp.bottom).offset(6)
            make.left.equalTo(titleLabel)
        }
    }

 
    func textFieldDidBeginEditing(_ textField: UITextField) {
        animateBorderColor(toColor: UIColorMake(0x2078D5).cgColor)
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        animateBorderColor(toColor: UIColorMake(0x2E3036).cgColor)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    private func animateBorderColor(toColor: CGColor) {
        let animation = CABasicAnimation(keyPath: "borderColor")
        animation.fromValue = contentView.layer.borderColor
        animation.toValue = toColor
        animation.duration = 0.25  // 动画时长
        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        contentView.layer.add(animation, forKey: "borderColor")
        contentView.layer.borderColor = toColor
    }
}
